<?php
date_default_timezone_set('Asia/Ho_Chi_Minh');
function loadPage($pagename){
	return $_SERVER['PHP_SELF']."?choose=".$pagename;
}
function cutstring($str)
{
	$str=substr($str,0,100);
	return $str;
}
function fewchars($s, $lenght) 
{
	
  if ($s=='' || $s==NULL) return $s;
if (is_array($s)) return $s;
$s = strip_tags(trim($s));
if(strlen($s) >= $lenght  && ($espacio = strpos($s, " ", $lenght ))) {
$s = substr($s, 0, $espacio).'...';
}
return $s;
}
/**
 * Example Usage
 * $content = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed 
 * do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
 *  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
 * Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 
 * cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
 * echo limit_words($content,20);
 * 
 * @param string <code>String<code>
 * @return string {limit word}
 * 
 */
function limit_words($string, $word_limit){
	$words = explode(" ",$string);
	return implode(" ",array_splice($words,0,$word_limit));
}
function format($str,$isComma=1){
	
	// 1 as visible comma, 0 as not comma
	// default as 1
	if($isComma==1){
		if (get_magic_quotes_gpc()==false) return "'".mysql_real_escape_string($str)."',";
		else return "'".$str."',";
	}
	elseif($isComma==4){
		return "'".htmlspecialchars($str, ENT_QUOTES)."',";
	}
	elseif($isComma==3){
		return "'".htmlspecialchars($str, ENT_QUOTES)."'";
	}
	else 
	{
	if (get_magic_quotes_gpc()==false) return "'".mysql_real_escape_string($str)."'";
	else return "'".$str."'";
	}
}

function formatCompare($str,$pos=0){
	// between front and background
	if($pos==0){
		return "'%".$str."%'";
	}else if($pos==-1){
		return "'%".$str."'";
	}else if($pos==1){
		return "'".$str."%'";
	}
	
}

// check empty
// 1 = empty
// 0 = string
function isEmpty($str){
	if($str==null || $str==' '){
		return 1;
	}else return 0;
}

// check box is checked
function isCheck($check){
	if($check==1){
		return 1;
	}else return 0;
}

// view tick check box when edit
function loadChecked($check){
		// 1 value =1 checked
		if($check==1){
				echo "value = 1 checked";
			}else echo "value = 1";
	}
function convert($str) 
{
	$chars = array(
          ''  =>  array('"','\'','  ',' -','- ',':','?','~','!','@','#','$','%','*',';',',','.','&'),
		 
        '-' =>  array(' ','  ','#','@','!','#','-','/'),
		'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă','A'),
		'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê','E'),
		'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị','I'),
		'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','O'),
		'u'	=>	array('U','ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư'),
		'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ','Y'),
		'd'	=>	array('đ','Đ','D'),
		'b'=>array('B'),
		'c'=>array('C'),
		'f'=>array('F'),
		'g'=>array('G'),
		'h'=>array('H'),
		'j'=>array('J'),
		'k'=>array('K'),
		'l'=>array('L'),
		'm'=>array('M'),
		'n'=>array('N'),
		'p'=>array('P'),
		'q'=>array('Q'),
		'r'=>array('R'),
		's'=>array('S'),
		't'=>array('T'),
		'v'=>array('V'),
		'x'=>array('X'),
		'z'=>array('Z'),
		'w'=>array('W'),
 	);

	foreach ($chars as $key => $arr) 
		foreach ($arr as $val)
			$str = str_replace($val,$key,$str);
	return $str;
}
// check login
function checkLogin(){
		if(!isset($_SESSION['admin'])){
			unset($_SESSION['admin']);
			header('location: login.php');
		}
	}
function checkYahoo($nick){
	// $filename="http://opi.yahoo.com/online?u=$nick&m=t&t=1";
	// $header_response = get_headers($filename, 1);
	// if ( strpos( $header_response[0], "404" ) !== false ) return false;
	// else{
		// $tmp = file_get_contents($filename);
	// //echo $tmp;die;
	// if($tmp=='00')
		// return false;
	// else
		// return true;
	// }
	$filename="http://opi.yahoo.com/online?u=$nick&m=t&t=1";

	$ch = curl_init();
	
	//Set curl to return the data instead of printing it to the browser.
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	//Set the URL
	curl_setopt($ch, CURLOPT_URL,$filename);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
	//Execute the fetch
	$data = curl_exec($ch);
	
	if ($data=='') return false;
	else{
	$tmp = file_get_contents($filename);
	//echo $tmp;die;
	if($tmp=='00')
	return false;
	else
	return true;
	}
	/**
	 * new code for check yahoo base on file content when use fopen function
	 * required online.gif file when check online with yahoo api
	 */
	 // set_error_handler(
	    // create_function(
	        // '$severity, $message, $file, $line',
	        // 'throw new ErrorException($message, $severity, $severity, $file, $line);'
	    // )
	// );
	 // try{
	 	// $online=fgets(fopen("images/online.gif","r"));
		// $status=fgets(fopen("http://opi.yahoo.com/online?u=$nick","r"));
		// if ($status==$online ) return true;
		// return false;
	 // }catch(Exception $e){
	 	// return false;
	 // }
	 // restore_error_handler();
}
function checkSkype($nick){
	$filename="http://mystatus.skype.com/$nick.num";
	if(!file_exists($filename)) return false;
	else{
	$tmp = file_get_contents($filename);
	//echo $tmp;die;
	if($tmp=='1')
		return false;
	else
		return	true;
	}		
}
function getUrlThumbnail($str){
		return str_replace($search = '/userupload/images/', $replace = '/userupload/_thumbs/Images/', $str); 
}
function formatMoney($number, $fractional=false) {
    if ($fractional) {
        $number = sprintf('%.2f', $number);
    }
    while (true) {
        $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
        if ($replaced != $number) {
            $number = $replaced;
        } else {
            break;
        }
    }
    return $number;
} 
 function getCurrentDayString() {
		return date("Y-m-d h:i");
	}
?>